package com.fengxu.gamescript.util

import android.animation.ValueAnimator
import android.app.Activity
import android.content.Context
import android.graphics.PixelFormat
import android.os.Build
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.xuexiang.xui.utils.DensityUtils
import org.jetbrains.anko.sdk27.coroutines.onTouch
import java.lang.IllegalStateException
import java.lang.RuntimeException

/**
 * 悬浮窗工具类
 */
object FloatUtils {

    /**
     * 获取窗口管理器对象
     * @param ctx 上下文对象
     */
    fun getWindowManager(ctx:Context):WindowManager{
        return when (ctx) {
            is Activity -> {
                (ctx as Activity).windowManager
            }
            is AppCompatActivity -> {
                (ctx as AppCompatActivity).windowManager
            }
            else -> {
                ctx.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            }
        }
    }

    /**
     * 获取默认的悬浮窗布局参数
     */
    fun getDefaultFloatParams():WindowManager.LayoutParams{
        val lp = WindowManager.LayoutParams()
        lp.type = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        }else{
            WindowManager.LayoutParams.TYPE_PHONE
        }
        lp.format = PixelFormat.RGBA_8888
        lp.flags =  WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS or
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
        lp.gravity = Gravity.LEFT or Gravity.TOP
        lp.width = -2
        lp.height =-2
        return lp
    }

    /**
     * 设置悬浮窗组件具有可移动的功能
     * @param windowManager 窗口管理器
     * @param view 待赋予滑动功能的组件
     * @param needSlider 是否需要自动贴边动画，默认false
     * @param onTouchEvent 触摸事件
     */
    fun setFloatViewCanMove(windowManager: WindowManager, view: View,
                            needSlider:Boolean = false, onTouchEvent:((MotionEvent)->Unit)? = null){
        var mTouchStartX = 0
        var mTouchStartY = 0
        val lp = view.layoutParams as WindowManager.LayoutParams
        view.onTouch { v, event ->
            onTouchEvent?.invoke(event)
            when(event.action){
                MotionEvent.ACTION_DOWN ->{
                    mTouchStartX = event.rawX.toInt()
                    mTouchStartY = event.rawY.toInt()
                }
                MotionEvent.ACTION_MOVE ->{
                    val xDiff = event.rawX.toInt() - mTouchStartX
                    val yDif = event.rawY.toInt() - mTouchStartY
                    mTouchStartX = event.rawX.toInt()
                    mTouchStartY = event.rawY.toInt()
                    lp.x += xDiff
                    lp.y += yDif
                    windowManager.updateViewLayout(view, lp)
                }
                // 释放后自动贴左边
                MotionEvent.ACTION_UP ->{
                    if(!needSlider) return@onTouch
                    val anim = if(lp.x > 50){
                        ValueAnimator.ofInt(lp.x, 0, 100, 0, 50, 0, 10, 0)
                    }else{
                        ValueAnimator.ofInt(lp.x, 0)
                    }
                    anim.addUpdateListener {
                        lp.x = anim.animatedValue as Int
                        windowManager.updateViewLayout(view, lp)
                    }
                    anim.start()
                }
            }
        }
    }

}